package la.bbs558.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import la.bbs558.dao.BbsBoardDao;
import la.bbs558.entity.BbsBoard;
import la.bbs558.entity.BbsClass;
import la.bbs558.entity.BbsSection;
import la.bbs558.entity.UserOption;
import la.bbs558.entity.vo.BbsBoardVO;
import la.bbs558.entity.enums.ClassIdType;
import la.bbs558.entity.params.BoardSearchPage;
import la.bbs558.entity.params.BoardUpdateParams;
import la.bbs558.satoken.StpAdminUtil;
import la.bbs558.service.BbsBoardService;
import la.bbs558.service.BbsClassService;
import la.bbs558.service.BbsSectionService;
import la.bbs558.service.UserOptionService;
import la.bbs558.util.Objs;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: Shanks
 * @date: 2023/12/15 22:43
 * @desc:
 */
@Service
@RequiredArgsConstructor
public class BbsBoardServiceImpl extends ServiceImpl<BbsBoardDao, BbsBoard> implements BbsBoardService {

    private final BbsBoardDao boardDao;

    private final BbsClassService bbsClassService;
    private final UserOptionService userOptionService;
    @Resource
    private BbsSectionService bbsSectionService;

    @Override
    public void listByPageForAdmin(BoardSearchPage searchPage) {
        boardDao.selectListByPageForAdmin(searchPage);
    }

    @Override
    public void addBoardForAdmin(BoardUpdateParams updateParams) {
        BbsBoard bbsBoard = new BbsBoard()
                .setCover(updateParams.getCover())
                .setLogo(updateParams.getLogo())
                .setBgImg(updateParams.getBgImg())
                .setBoardName(updateParams.getBoardName())
                .setBoardDesc(updateParams.getBoardDesc())
                .setBoardKeywords(updateParams.getBoardKeywords())
                .setClassCnt(updateParams.getBbsClassList().size())
                .setSort(999)
                .setCreateUser(StpAdminUtil.getLoginIdAsLong());
        save(bbsBoard);
        List<BbsClass> collect = updateParams.getBbsClassList().stream().map(bbsClass ->
                new BbsClass()
                        .setClassName(bbsClass.getClassName())
                        .setBoardId(bbsBoard.getId())
        ).collect(Collectors.toList());
        bbsClassService.saveBatch(collect);
    }

    @Override
    public void updateBoardForAdmin(BoardUpdateParams updateParams) {
        BbsBoard bbsBoard = new BbsBoard()
                .setId(updateParams.getId())
                .setCover(updateParams.getCover())
                .setLogo(updateParams.getLogo())
                .setBgImg(updateParams.getBgImg())
                .setBoardName(updateParams.getBoardName())
                .setBoardDesc(updateParams.getBoardDesc())
                .setBoardKeywords(updateParams.getBoardKeywords())
                .setClassCnt(updateParams.getBbsClassList().size());
        boolean result = updateById(bbsBoard);
        Objs.isTrue(result, "更新失败");
        // 查询旧的分类列表
        List<BbsClass> bbsClassList = bbsClassService.lambdaQuery()
                .eq(BbsClass::getBoardId, updateParams.getId())
                // 只查询普通分类
                .eq(BbsClass::getType, 1)
                .list();
        Map<Long, Boolean> oldBbsClassIdMap = new HashMap<>(bbsClassList.size());
        for (BbsClass bbsClass : bbsClassList) {
            oldBbsClassIdMap.put(bbsClass.getId(), true);
        }
        updateParams.getBbsClassList().forEach(bbsClass -> {
            if (bbsClass.getId() == null) {
                // 新增
                bbsClass.setBoardId(updateParams.getId());
                bbsClassService.save(bbsClass);
            } else {
                // 更新
                oldBbsClassIdMap.remove(bbsClass.getId());
                bbsClassService.updateById(bbsClass);
            }
        });
        // 删除
        for (Long oldId : oldBbsClassIdMap.keySet()) {
            bbsClassService.removeById(oldId);
        }
    }

    @Override
    public void deleteBoardByIdForAdmin(Long id) {
        removeById(id);
        bbsClassService.lambdaUpdate()
                .eq(BbsClass::getBoardId, id)
                .remove();
    }

    @Override
    public BbsBoardVO getBoardById(Long id, boolean isAdmin) {
        BbsBoard bbsBoard = getById(id);
        Objs.isTrue(bbsBoard != null, "板块已不存在");
        BbsSection section = bbsSectionService.getById(bbsBoard.getSectionId());
        Objs.isTrue(section != null, "分区已不存在");
        List<BbsClass> list = bbsClassService.lambdaQuery()
                .select(BbsClass::getId, BbsClass::getClassName, BbsClass::getPostCnt, BbsClass::getType)
                .eq(BbsClass::getBoardId, id)
                .eq(BbsClass::getStatus, 1)
                .orderByAsc(BbsClass::getType)
                .list();
        if (isAdmin) {
            // 是管理员，1代表是普通分类
            list = list.stream().filter(bbsClass -> bbsClass.getType() == 1).collect(Collectors.toList());
        } else {
            // 全部
            list.add(0, new BbsClass()
                    .setClassName(ClassIdType.ALL.getName())
                    .setId(ClassIdType.ALL.getId()));
            // 已结束
            list.add(new BbsClass()
                    .setClassName(ClassIdType.END.getName())
                    .setId(ClassIdType.END.getId()));
            // 精华
            list.add(new BbsClass()
                    .setClassName(ClassIdType.ESSENCE.getName())
                    .setId(ClassIdType.ESSENCE.getId()));
        }
        Integer collect = userOptionService.lambdaQuery()
                .eq(UserOption::getBoardId, id)
                .eq(UserOption::getType, 2)
                .count();
        return new BbsBoardVO()
                .setId(bbsBoard.getId())
                .setSectionId(bbsBoard.getSectionId())
                .setSectionName(section.getSectionName())
                .setClassCnt(bbsBoard.getClassCnt())
                .setCollectCnt(collect)
                .setPostCnt(bbsBoard.getPostCnt())
                .setTopicCnt(bbsBoard.getTopicCnt())
                .setBoardName(bbsBoard.getBoardName())
                .setBoardKeywords(bbsBoard.getBoardKeywords())
                .setBoardDesc(bbsBoard.getBoardDesc())
                .setCover(bbsBoard.getCover())
                .setLogo(bbsBoard.getLogo())
                .setBgImg(bbsBoard.getBgImg())
                .setBbsClassList(list);
    }
}
